<?php

App::uses('AppModel', 'Model');

/**

 * PropertyType Model

 *

 */

class PropertyType extends AppModel {



/**

 * Use table

 *

 * @var mixed False or table name

 */

	public $useTable = 'property_type';



/**

 * Display field

 *

 * @var string

 */

	public $displayField = 'title';
  protected $_cacheKey = 'property_type';


/**

 * Validation rules

 *

 * @var array

 */

	public $validate = array(

		'title' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				'message' => 'Title required',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),

		),

	);
    public function beforeSave($options = array()) {//pr($this->data); //exit;
        if (!empty($this->data[$this->alias]['title']) && empty($this->data[$this->alias]['slug'])) {
            if (!empty($this->data[$this->alias][$this->primaryKey])) {
                $this->data[$this->alias]['slug'] = $this->generateSlug($this->data[$this->alias]['title'], $this->data[$this->alias][$this->primaryKey]);
            } else {
                $this->data[$this->alias]['slug'] = $this->generateSlug($this->data[$this->alias]['title']);
            }
        }
        return true;
    }


		public function afterSave($created, $options = array()) {
			parent::afterSave($created, $options = array());
			Cache::delete($this->_cacheKey);
		}
		public function afterDelete() {
			Cache::delete($this->_cacheKey);
		}

	  public function getPropertyType() {
		if ($getPropertyType = Cache::read($this->_cacheKey)) {
		  return $getPropertyType;
		}
		$getPropertyType = $this->find('list', array('conditions'=>array('PropertyType.active'=>1),
		));
		Cache::write($this->_cacheKey, $getPropertyType);
		return $getPropertyType;
	  }


}

